<?php

namespace Code;

class Helper
{

    // private static $instance;

    private function __construct() {
    }

    public static function getInstance() {
        static $instance = null;
        if (is_null($instance)) {
            $instance = new Helper();
        }
        return $instance;
    }



    function include_component($file, $parameters = array(), $once = false) {
        if (is_array($parameters) && !empty($parameters)) {
            extract($parameters, EXTR_PREFIX_SAME, 'code');
        }
        if ($once) {
            return include_once $file;
        } else {
            return include $file;
        }
    }



    function include_model($bundle) {
        $path = BASE_DIR.'model/'.$bundle.'.php';
        if (file_exists($path)) {
            return $this->include_component($path, array(), true);
        } else {
            return false;
        }
    }



    function include_template($file, $parameters = array()) {
        if (!is_array($parameters)) {
            return false;
        }
        $path_to_file = explode('/', $file);
        if (count($path_to_file)!=2) {
            return false;
        }
        $path = BASE_DIR.'/bundles/'.$path_to_file[0].'/templates/'.$path_to_file[1].'.php';
        if (!file_exists($path)) {
            return false;
        }
        ob_start();
        $this->include_component($path, $parameters);
        return ob_get_clean();
    }



    function include_layout($parameters = array()) {
        ob_start();
        $this->include_component(BASE_DIR.'bundles/layout.php', $parameters);
        return ob_get_clean();
    }

    function upload_file($bundle, $file) {
        $directorio = BASE_DIR.PUBLIC_DIR.'/uploads/'.$bundle.'/';
        $archivo = date('Y-m-d_H-i-s_').$file->getClientOriginalName();
        if ($file->move($directorio, $archivo)) {
            return $archivo;
        } else {
            return false;
        }
    }

    function get_order_links(array $criterias, $order_by = null, $order_type = null) {
        foreach ($criterias as $criteria) {
            $order[$criteria] = '&orden_t=asc';
            $order[$criteria.'_icono'] = '';
        }
        if (!is_null($order_by)) {
            $order[$order_by] = ($order_type=='asc' ? '&orden_t=desc' : '&orden_t=asc' );
            $order[$order_by.'_icono'] = ($order_type=='asc' ? '&nbsp;<i class="icon-chevron-up icon-white"></i>' : '&nbsp;<i class="icon-chevron-down icon-white"></i>' );
        }
        return $order;
    }

    function build_uri(array $parameters = null) {
        $request = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
        if (!is_null($parameters)) {
            foreach ($parameters as $parameter) {
                $request->query->remove($parameter);
            }
        }
        $uri = $request->getPathInfo();
        $i = 0;
        foreach ($request->query->all() as $parameter => $value) {
          if (!empty($value)) {
            $glue = (++$i==1 ? '?' : '&' );
            $uri .= $glue.$parameter.'='.$value;
          }
        }
        $glue = (++$i==1 ? '?' : '&' );
        $uri .= $glue.$parameters[0].'=';
        return $uri;
    }

}

?>
